package com.room.controller.verify;

import com.room.pojo.DormResult;
import com.room.pojo.User;
import com.room.pojo.updatePassWord;
import com.room.service.SendemailService;
import com.room.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;



/**
 * @创建人 lin
 * @创建时间 2019/10/9
 * @描述
 */
@RestController
@RequestMapping("/email/manager")
public class PwdController {
    @Autowired
    private SendemailService sendemailService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;



    @RequestMapping(value = "/findpwd",method = RequestMethod.GET)
    public DormResult sendEmail(@RequestParam String name) {
        User user = userService.getUserByUsername(name);
        String to = user.getEmail();
        Random random = new Random();
        String msg = "";
        for (int i = 0; i < 6; i++) {
            int x = random.nextInt(10);
            msg += x;
        }
        ValueOperations ops = redisTemplate.opsForValue();
        Map<String, Object> map = new HashMap<>();
        map.put("code", msg);
        map.put("date", new Date().getTime());
        ops.set(name + "code", map);

        try {
            sendemailService.sendEmail(to, msg);
        } catch (Exception e) {
            System.out.println(e);
            return DormResult.error("邮件发送失败");
        }
        return DormResult.ok("邮件已发送");
    }



   @RequestMapping(value = "/check",method = RequestMethod.GET)
    public DormResult check(@RequestParam String username,@RequestParam String code) {
       System.out.println(username);
       System.out.println(code);
        ValueOperations ops = redisTemplate.opsForValue();
        Map<String, Object> map = (Map<String, Object>) ops.get(username + "code");
        long date = (long) map.get("date");
        long now = new Date().getTime();
        if ((now - date) / 1000 <= 60) {
            String redisCode = (String) map.get("code");
            if (redisCode.equals(code)) {
                redisTemplate.delete(username + "code");
                return DormResult.ok("验证成功");
            }
        } else {
            redisTemplate.delete(username + "code");
            return DormResult.error("验证码以过期,请重新获取");
        }
        return DormResult.error("验证码错误,请重新输入");
    }

    @RequestMapping(value = "/updatebypwd",method = RequestMethod.PUT)
    public DormResult updateByPwd(updatePassWord map) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String username = map.getName();
        User user = userService.getUserByUsername(username);
        String pwd = map.getPwd();
        if (bCryptPasswordEncoder.matches(pwd, user.getPassword())) {
            String encode = bCryptPasswordEncoder.encode(map.getPwdnow());
            int i = userService.updatepewd(username, encode);
            if (i == 1) {
                return DormResult.ok("修改成功");
            }else
            {  return DormResult.error("修改失败");}
        }
        return DormResult.error("帐号与原密码不匹配");
    }




@RequestMapping(value = "/codesuccess",method = RequestMethod.GET)
public DormResult update(@RequestParam String username,@RequestParam String pwdnow) {
    BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
    String encode = bCryptPasswordEncoder.encode((String) pwdnow);
    int i = userService.updatepewd(username, encode);
    if (i == 1) {
        return DormResult.ok("修改成功");
    }
    return DormResult.error("修改失败");
}

}